package kr.co.mustgo.controller.membership;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.http.HttpSession;

import kr.co.mustgo.domain.membership.Admin;
import kr.co.mustgo.domain.membership.Membership;
import kr.co.mustgo.model.service.membership.MembershipService;
import kr.co.mustgo.model.service.membership.AdminService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/admin/") //class에 지정한 URL - 모든 Handler 메소드들의 URL 앞에 붙이는 공통 경로 지정
public class AdminController {
	
	
	@Autowired
	private AdminService service;
	
	/***************관리자 로그인 처리***************/
	//로그인 form을 제공
	@RequestMapping(value="login.do", method=RequestMethod.GET)
	public String loginForm(){
		return "tiles2/admin/adminLogin_form.jsp";
	}
	
	//로그인 처리
	@RequestMapping(value = "login.do", method = RequestMethod.POST)
	public String login(String id, String password, HttpSession session,
			ModelMap map) throws SQLException {
		Admin admin = service.getAdminById(id);
		String url = null;
		if (admin != null) {
			if (admin.getPassword().equals(password)) {
				session.setAttribute("adminLogin_info", admin);
				url = "tiles2/admin/membership_management.jsp";
			} else {
				map.addAttribute("error_message", "로그인 실패");
				url = "tiles2/admin/adminLogin_form.jsp";
			}
		} else {
			map.addAttribute("error_message", "로그인 실패");
			url = "tiles2/admin/adminLogin_form.jsp";
		}
		return url;
	}
	
	/***************로그아웃 처리***************/
	@RequestMapping(value = "logout.do", method=RequestMethod.GET)
	public String logout(HttpSession session){
		//새션을 소멸 -> main.jsp
		session.invalidate(); 
		//return "/WEB-INF/view/membership/logout_success.jsp";
		//return "redirect:/index.jsp";
		return "redirect:/";
	}

	/***************관리자페이지***************/
	@RequestMapping(value = "admin.do", method=RequestMethod.GET)
	public String adminPage(){
		return "tiles2/admin/admin.jsp";
	}
	
	/***************관리자 가입폼***************/
	@RequestMapping(value="adminJoin_form.do", method=RequestMethod.GET)
	public String adminJoinForm(){
		return "tiles2/admin/adminJoin_form.jsp";
	}
	
	/*아이디 중복 체크*/
	@RequestMapping(value = "adminIdCheck.do", method = RequestMethod.POST)
	public String adminIdCheck(String id, ModelMap map) throws SQLException, IOException {
		System.out.println("id = " + id);
		Admin admin = service.getAdminById(id);
		System.out.println(admin);
		
			map.addAttribute("admin", admin);
			
		return "jsonView";
	}
	
	//관리자 가입 완료
	@RequestMapping(value="adminJoin.do", method=RequestMethod.POST)
	public String joinMembership(Admin admin, HttpSession session) throws Exception{
		service.joinAdmin(admin);
		session.setAttribute("adminJoin_info", admin);
		//join_success.jsp
		return "tiles2/admin/adminJoin_success.jsp";
	}
	
	
}//end class
